import random

from locals import *
import objects
import walls

FLOOR = 0
CHASM = 1
WALL = 2

class Chasm(objects.Tile):
	graphic = "chasm.png"
	_symbols = "."
	
	type = CHASM
	
	_passable = False
	_match_edges = False
	
	_damage = ("chasm",)
	
	def step(self, stepper):
		stepper.kill(self, self._damage)
		
	def stand(self, stander):
		stander.kill(self, self._damage)

class Wall(objects.Tile):
	graphic = "wall.png"
	_symbols = "#"
	
	type = WALL
	
	_passable = False
	_match_edges = True

class Floor(objects.Tile):
	graphic = "floor.png"
	_symbols = "_"
	
	type = FLOOR
	
	_passable = True
	_match_edges = False

class Exit(Floor):
	graphic = "exit.png"
	_symbols = "e"
	
	_passable = True
	_match_edges = False

	def stand(self, stander):
		if stander.type == "player":
			self.map.exit()

class Water(Floor):
	graphic = "water.png"
	_symbols = "~"
	
	_passable = True
	_match_edges = False
	
	_damage = ("water",)
	
	def step(self, stander):
		stander.kill(self, self._damage)
		
	def stand(self, stander):
		stander.kill(self, self._damage)
		
	def kill(self, killer, damage_types):
		if damage_types[0] == "blunt":
			self.map.objects.append(walls.Dirt(self.map, self.position))
			self.map._index_objects()
			self.map._order_objects()
			self._transform(Floor)

class Gravel(Floor):
	graphic = "gravel.png"
	_symbols = "@"
	
	_passable = True
	_match_edges = False
	
	def passable(self, object):
		return not object.hostile
		
class Ice(Floor):
	graphic = "ice.png"
	_symbols = "\/lr+"
	
	_passable = True
	_match_edges = False
	
	slide_speed = 4
	
	_movement_transform = {
		0: {0: 1, 1: 1, 2: 2, 3: 2},
		1: {1: 2, 2: 2, 0: 3, 3: 3},
		2: {1: 0, 2: 3, 3: 3, 0: 0},
		3: {3: 0, 0: 0, 1: 1, 2: 1},
	}
	
	_passability = {
		0: {0: True, 1: False, 2: False, 3: True},
		1: {0: True, 1: True, 2: False, 3: False},
		2: {0: False, 1: True, 2: True, 3: False},
		3: {0: False, 1: False, 2: True, 3: True},
	}
	
	def __init__(self, map, position, direction):
		super(Ice, self).__init__(map, position)
		self.direction = direction

	def save(self, variables):
		for key, value in variables["directions"].iteritems():
			if value == self.direction:
				direction = key
		
		return "%s, %s" % (super(Ice, self).save(variables), direction)

	def draw(self, surface, configuration=None):
		surface.blit(self.graphic.subsurface((self.direction + 1) * 32, 0, 32, 32), self._draw_position())

	def passable(self, passer):
		if self.direction != -1:
			return self._passability[self.direction][passer.direction]
		else:
			return True

	def step(self, stepper):
		stepper.frozen = (True, True)

	def stand(self, stander):
		if not hasattr(stander, "float") or not stander.float:
			stander.frozen = (True, True)
			
			if self.direction != -1:
				stander.direction = self._movement_transform[self.direction][stander.direction]
			
			if not stander.move(movements[stander.direction], self.slide_speed):
				stander.direction += 2
				if stander.direction > 3:
					stander.direction -= 4

"""Seperate ice into five seperate tiles for use in map arrays:"""
def IceTopRight(map, position): return Ice(map, position, 0)
def IceBottomRight(map, position): return Ice(map, position, 1)
def IceBottomLeft(map, position): return Ice(map, position, 2)
def IceTopLeft(map, position): return Ice(map, position, 3)
def IceSheet(map, position): return Ice(map, position, -1)

class Force(Floor):
	graphic = "force.png"
	_symbols = ">v<^o"
	
	_passable = True
	_match_edges = False
	
	_freezes = {
		-1: (False, False),
		0: (True, False),
		1: (False, True),
		2: (True, False),
		3: (False, True),
	}
	
	push_speed = 4
	
	def __init__(self, map, position, direction):
		super(Force, self).__init__(map, position)
		self.direction = direction

	def save(self, variables):
		for key, value in variables["directions"].iteritems():
			if value == self.direction:
				direction = key
		
		return "%s, %s" % (super(Force, self).save(variables), direction)

	def draw(self, surface, configuration=None):
		surface.blit(self.graphic.subsurface((self.direction + 1) * 32, 0, 32, 32), self._draw_position())

	#def step(self, stepper):
	#	stepper.frozen = (True, True)

	def stand(self, stander):
		if not hasattr(stander, "float") or not stander.float:
			if self.direction == -1:
				direction = random.randint(0, 3)
			else:
				direction = self.direction
				
			stander.move(movements[direction], self.push_speed)
			stander.frozen = self._freezes[direction]
				
"""Seperate force into five seperate tiles for use in map arrays:"""
def ForceUp(map, position): return Force(map, position, 3)
def ForceRight(map, position): return Force(map, position, 0)
def ForceDown(map, position): return Force(map, position, 1)
def ForceLeft(map, position): return Force(map, position, 2)
def ForceRandom(map, position): return Force(map, position, -1)

class MysteryWall(Wall):
	graphic = "mystery_wall.png"
	_symbols = "?!"
	
	_passable = False
	_match_edges = True
	
	type = 50
	
	def __init__(self, map, position, fake):
		super(MysteryWall, self).__init__(map, position)
		self.fake = fake
	
	def save(self, variables):
		for key, value in variables["fake_wall"].iteritems():
			if value == self.fake:
				fake = key
		
		return "%s, %s" % (super(MysteryWall, self).save(variables), fake)

	def symbol(self):
		return self._symbols[self.fake]

	def push(self, pusher, movement, speed):
		if self.fake:
			self._transform(Floor)
			return True
		else:
			self._transform(Wall)
			return False
			
def FakeWall(map, position): return MysteryWall(map, position, True)
def RealWall(map, position): return MysteryWall(map, position, False)
